#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGraphicsPixmapItem>
#include <QFileDialog>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    astroScene scene(this);
    //scenes.clear();
    //scenes.push_back(&scene);
    scenes = new astroScene*[1];
    scenes[0] = new astroScene(this);

    ui->graphicsView->setScene(scenes[0]);
    dScaleX = dScaleY = 1.0;

    floater = new FloatingDialog(this);
    floater->show();

    //connect(&scenes->at(0), SIGNAL(newMouse(QPointF)), floater, SLOT(setNewPixel(QPointF)));
    //connect(&scenes->at(0), SIGNAL(newRGB(int, int, QRgb)), floater, SLOT(setNewRGB(int, int, QRgb)));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::Load(int nIndex)
{
    //scenes[nIndex]->qp.load("C:\\Users\\willif\\Documents\\astro\\IMG_0565.JPG");
    //scenes[nIndex]->image = scenes[nIndex]->qp.toImage();
    QGraphicsPixmapItem* qItem = new QGraphicsPixmapItem;
    qItem->setPixmap(scenes[nIndex]->qp);
    ui->graphicsView->scene()->clear();
    dScaleX = dScaleY = 1.0;
    ui->graphicsView->scale(dScaleX, dScaleY);
    ui->graphicsView->setTransform(QTransform());
    ui->graphicsView->setScene(scenes[nIndex]);
    ui->graphicsView->scene()->addItem(qItem);

    connect(scenes[nIndex], SIGNAL(sigMouse(QPointF)), floater, SLOT(slotNewPixel(QPointF)));
    connect(scenes[nIndex], SIGNAL(sigRGB(int, int, QRgb)), floater, SLOT(slotNewRGB(int, int, QRgb)));
    connect(scenes[nIndex], SIGNAL(sigFile(int, int, QRgb)), floater, SLOT(slotNewRGB(int, int, QRgb)));
    connect(scenes[nIndex], SIGNAL(sigStatus(QString)), floater, SLOT(slotStatus(QString)));
    connect(scenes[nIndex], SIGNAL(sigStar(int,int)), floater, SLOT(slotStar(int,int)));
}


void MainWindow::on_pbLoadSequence_clicked()
{
    QFileDialog dialog(this);
    dialog.setDirectory(QDir::homePath());
    dialog.setFileMode(QFileDialog::ExistingFile);
    dialog.setNameFilter(trUtf8("Splits (*.jpg)"));
    dialog.setFileMode(QFileDialog::ExistingFiles);
    QStringList fileNames;
    if( !dialog.exec() )
        return;

    fileNames = dialog.selectedFiles();
    ui->listWidget->addItems(fileNames);

    int nCount = fileNames.size();
    //scenes.clear();
    delete [] scenes;
    scenes = new astroScene*[nCount];
    for(int i=0; i<nCount; i++)
    {
        scenes[i] = new astroScene(this);
        connect(scenes[i], SIGNAL(sigMouse(QPointF)), floater, SLOT(slotNewPixel(QPointF)));
        connect(scenes[i], SIGNAL(sigRGB(int, int, QRgb)), floater, SLOT(slotNewRGB(int, int, QRgb)));
        connect(scenes[i], SIGNAL(sigFile(int, int, QRgb)), floater, SLOT(slotNewRGB(int, int, QRgb)));
        connect(scenes[i], SIGNAL(sigStatus(QString)), floater, SLOT(slotStatus(QString)));
        connect(scenes[i], SIGNAL(sigStarCount(int)), floater, SLOT(slotStarCount(int)));
        connect(scenes[i], SIGNAL(sigStar(int,int)), floater, SLOT(slotStar(int,int)));
        connect(floater, SIGNAL(sigCenterOn(int,int)), this, SLOT(slotCenterOn(int,int)));

        scenes[i]->Load(fileNames[i]);
    }
    Load(0);
}

void MainWindow::onListChanged()
{
}

void MainWindow::on_listWidget_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
{
    Load(current->listWidget()->row(current));
}

void MainWindow::on_listWidget_itemChanged(QListWidgetItem *item)
{

}

void MainWindow::slotCenterOn(int x, int y)
{
    ui->graphicsView->centerOn(x,y);
}

void MainWindow::on_pbZoomIn_clicked()
{
    ui->graphicsView->scale(1.1,1.1);
}

void MainWindow::on_pbZoomOut_clicked()
{
    ui->graphicsView->scale(1/1.1,1/1.1);
}
